package com.lanmao.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanmao.blog.entity.Links;
import com.lanmao.blog.service.LinksService;
import com.lanmao.blog.utils.R;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lanmao
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/links")
public class LinksController {
    @Autowired
    LinksService linksService;
    /**
     * 新增友链
     * @param links
     * @return
     */
    @PostMapping("/saveLink")
    public R saveLink(@RequestBody Links links){
        links.setCreateTime(LocalDateTime.now());
        Boolean result =linksService.save(links);
        if (result){
            return R.ok().message("添加友链成功！");
        }else {
            return R.error().message("添加友链失败！");
        }
    }
    @GetMapping("/getAllLink")
    public R getAllLink(){
        List<Links> linksList = linksService.list();
        return R.ok().data("data",linksList);
    }
    /**
     * 友链的分页查询
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getLinksByPage")
    @ApiOperation("友链的分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "每页的数量"),
    })
    public R getLinksByPage(Long current, Long size){
        Page<Links> linksPage= linksService.pageLinks(current, size);
        return R.ok().data("data",linksPage);
    }

    /**
     * 通过名称查找友链
     * @param name
     * @return
     */
    @GetMapping("/getLinksByName")
    public R getLinksByName(String name){
        QueryWrapper<Links> queryWrapper = new QueryWrapper<>();
        if (!name.equals("")&&!name.isEmpty()){
            queryWrapper.like("blog_name",name);
        }
        List<Links> links = linksService.list(queryWrapper);
        return R.ok().data("data",links);
    }

    /**
     * 更新友链
     * @param links
     * @return
     */
    @PutMapping("/updateLink")
    public R updateLink(@RequestBody Links links){
        boolean flag = linksService.updateById(links);
        if (flag){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }

    /**
     * 删除友链
     * @param id
     * @return
     */
    @DeleteMapping("deleteLinkById")
    public R deleteLinkById(Long id){
        boolean flag = linksService.removeById(id);
        if (flag){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    /**
     * 根据id查询友链
     * @param id
     * @return
     */
    @GetMapping("/getLinksById")
    public R getLinksById(Long id){
        Links links = linksService.getById(id);
        return R.ok().data("data",links);
    }
}
